/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef COMMON_SHELL_H
#define COMMON_SHELL_H

#include <string>
#include <vector>
#include <map>
#include <functional>
#include <thread>
#include <mutex>
#include "utils_ipc.h"
#include "utils_noncopyable.h"

namespace El {
namespace Common {

class Shell {
    DISALLOW_COPY_AND_MOVE(Shell);

public:
    using CommandFunction = std::function<void(const std::string &)>;

    static Shell &GetInstance();

    Shell();
    ~Shell();
    void RegisterCommand(const std::string &name, CommandFunction command);
    void Start();
    void Stop();

    // 新增方法：直接执行命令并返回结果
    std::string ExecuteCommand(const std::string &input);

private:
    void RegisterDefaultCommands();
    void RunShell();
    void ProcessCommand(const std::string &input);

private:
    std::map<std::string, CommandFunction> m_commands;
    static std::vector<std::string> s_command_names;
    std::thread m_thread;
    bool m_running;
    std::mutex m_mutex;

    // IPC服务端
    Utils::Server m_ipcServer;
};

} // namespace Common
} // namespace El

#endif // COMMON_SHELL_H